package m1;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

public class Consumer {
    public static void main(String[] args) throws MQClientException {
        // 新建消费者实例
        DefaultMQPushConsumer c = new DefaultMQPushConsumer("consumerGroup1");
        // 设置 name server
        c.setNamesrvAddr("192.168.64.141:9876");
        // 指定从哪儿订阅消息
        // Tag1
        // Tag1 || Tag2 || Tag3
        // *
        c.subscribe("Topic1", "Tag1");
        // 设置消息的监听器（回调对象）
        // Concurrently 并发的消息监听，会启动多个线程同时并行接收处理消息
        c.setMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(
                    List<MessageExt> msgs,
                    ConsumeConcurrentlyContext context) {
                for (MessageExt msg : msgs) {
                    byte[] a = msg.getBody();
                    String s = new String(a);
                    System.out.println("收到："+s);
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                //稍后要重新消费，回滚、回退消息
                //return ConsumeConcurrentlyStatus.RECONSUME_LATER;
            }
        });

        // 启动
        c.start();

    }
}
